//
//  SLHomeViewController.m
//  StarLight
//
//  Created by apple on 16/7/30.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLHomeViewController.h"
#import "SLInformationViewModel.h"
#import <MJRefresh.h>
#import "SLMineViewModel.h"
#import "SLHomeHeadView.h"
#import "SLHomeCollectionViewCell.h"
#import "SLNearViewController.h"
#import "SDCycleScrollView.h"
#import "SLAttentionCargoUserViewController.h"
#import "SLMessageCenterViewController.h"
#import "SLSpecialOffersViewController.h"
#import "BKJH5ViewController.h"
#import "SLSearchViewController.h"
#import "SLUserStateViewController.h"
#import "SLAddRouteViewController.h"
#import "SLAddBoatViewController.h"
#import "SLAddShippingViewController.h"
#import <STPopup/STPopup.h>
#import "SLUpdateWatercraftViewController.h"

@interface SLHomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,SDCycleScrollViewDelegate>

@property (nonatomic ,strong) UICollectionView *collectionView;

@property (nonatomic, strong) SLInformationViewModel *infoViewModel;

@property (nonatomic, strong) SLMineViewModel *mineViewModel;

@property (nonatomic ,strong) NSMutableArray *photoList;

@property (nonatomic ,strong) NSMutableArray *scrollListImg;

@property (nonatomic, strong) UIButton *changePierBtn;

@property (nonatomic ,strong) UIButton *userStateBtn;



@end

@implementation SLHomeViewController

- (instancetype)init
{
    if (self = [super init]) {
        self.mineViewModel = [[SLMineViewModel alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.collectionView];
    [self.naviHeader addSubview:self.changePierBtn];
    [self.naviHeader addSubview:self.userStateBtn];
    [self addConstrains];
    [self addObservers];
    // Do any additional setup after loading the view.
}



- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if ([[DEFAULTS objectForKey:@"userType"]  isEqual: @(0)]) {
       [_userStateBtn setTitle:@"船东版" forState:UIControlStateNormal];
    }
    if ([[DEFAULTS objectForKey:@"userType"]  isEqual: @(1)]){
        [_userStateBtn setTitle:@"货主版" forState:UIControlStateNormal];
    }
    self.view.backgroundColor = [UIColor defeutBackgroudColor];
}

/**
 *  改变货主和船住的身份
 */
- (void)changeUserState{
    SLUserStateViewController *userState = [[SLUserStateViewController alloc] init];
    userState.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:userState animated:YES];
}
- (void)addObservers{
    @weakify(self)
    [RACObserve(self.mineViewModel, indexModel) subscribeNext:^(SLMineUserIndexModel *model) {
        @strongify(self)
        if (model.banner.count > 0) {
            [self configSelfWithIndexModel:model];
            if (self.mineViewModel.indexModel.banner.count != self.scrollListImg.count) {
                for (int i = 0; i < self.mineViewModel.indexModel.banner.count; i++) {
                    Banner *bannerModel = self.mineViewModel.indexModel.banner[i];
                    [self.scrollListImg addObject:bannerModel.imagurl];
                }
            }
            [self.collectionView reloadData];
        }
    }];
    
    [[RACObserve(self.infoViewModel, bannerList) filter:^BOOL(NSMutableArray *value) {
        return value != nil;
    }] subscribeNext:^(NSMutableArray *list) {
        @strongify(self)
        [self.collectionView reloadData];
    }];
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:kHZTYPE object:nil] subscribeNext:^(id x) {
        @strongify(self)
        [self appendBtn];
    }];
    
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:kCDTYPE object:nil] subscribeNext:^(id x) {
        @strongify(self)
        [self addCdType];
    }];
}

// 数据更新的时候 更新 UI
- (void)configSelfWithIndexModel:(SLMineUserIndexModel *)model{
    
   
}

#pragma mark - addMethed
- (void)SLNearView{
    SLNearViewController *near = [[SLNearViewController alloc] init];
    near.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:near animated:YES];
}
- (void)SLAttentCargoUser{
    SLAttentionCargoUserViewController *attention = [[SLAttentionCargoUserViewController alloc] init];
    attention.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:attention animated:YES];
}
- (void)showMessageCenter{
    SLMessageCenterViewController *msgCenterVC = [SLMessageCenterViewController new];
    msgCenterVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:msgCenterVC animated:YES];
}
- (void)SLSpecialOffers{
    SLSpecialOffersViewController *specialOffers = [SLSpecialOffersViewController new];
    specialOffers.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:specialOffers animated:YES];
}
- (void)showSearchVC{
    SLSearchViewController *searchVC = [SLSearchViewController new];
    searchVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:searchVC animated:YES];
}
- (void)addRouteView{
    SLAddRouteViewController *addRoute = [[SLAddRouteViewController alloc] init];
    addRoute.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:addRoute animated:YES];
}
- (void)appendBtn{
    SLAddBoatViewController *addBoatView = [[SLAddBoatViewController alloc]init];
    addBoatView.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:addBoatView animated:YES];
}
- (void)addCdType{
    SLAddShippingViewController *add = [[SLAddShippingViewController alloc] init];
    @weakify(self)
    add.addUpViewBlock = ^(){
        @strongify(self)
        SLAddBoatViewController *addBoatView = [[SLAddBoatViewController alloc]init];
        addBoatView.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:addBoatView animated:YES];
    };
    add.updateUpViewBlock = ^(){
        SLUpdateWatercraftViewController *watercraftVc = [[SLUpdateWatercraftViewController alloc]init];
        watercraftVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:watercraftVc animated:YES];
    };
    
    
    STPopupController *popupController = [[STPopupController alloc] initWithRootViewController:add];
    [popupController setNavigationBarHidden:YES animated:YES];
    popupController.containerView.layer.cornerRadius = 4;
    [popupController presentInViewController:self];
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        if ([cell isKindOfClass:[SLHomeCollectionViewCell class]]) {
            SLHomeCollectionViewCell *displayCell = (SLHomeCollectionViewCell *)cell;
            [displayCell cellForData:self.photoList[indexPath.row][@"text"] img:self.photoList[indexPath.row][@"image"]];
        }
    }else if (indexPath.section == 0){
        if ([cell isKindOfClass:[SLHomeHeadView class]]) {
            SLHomeHeadView *homeCell = (SLHomeHeadView *)cell;
            [homeCell cellForPicModel:self.scrollListImg];
        }
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        SLHomeHeadView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor whiteColor];
        cell.layer.cornerRadius = 4;
        cell.layer.masksToBounds = YES;
        cell.pushConentView = ^(NSInteger index)
        {
            Banner *bannerModel = self.mineViewModel.indexModel.banner[index];
            BKJH5ViewController *h5View = [[BKJH5ViewController alloc] init];
            h5View.h5Url = bannerModel.conturl;
            h5View.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:h5View animated:YES];
        };
        return cell;
    }
    
    SLHomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 0:
                [self SLNearView];
                break;
            case 1:
                [self addRouteView];
                break;
            case 2:
                [self showSearchVC];
                break;
            case 3:
                [self SLAttentCargoUser];
                break;
            case 4:
                [self showMessageCenter];
                break;
            case 5:
                [self SLSpecialOffers];
                break;
                
            default:
                break;
        }
    }
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return 6;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0) {
        return UIEdgeInsetsMake(10 , 10, 0, 10);
    }
    return UIEdgeInsetsMake(10 , 10, 10, 10);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(SCREEN_WIDTH - 20, 161 * MIX_H);
    }
    return CGSizeMake((SCREEN_WIDTH / 2) - 11, (SCREEN_HEIGHT - (161 * MIX_H) - 50) / 4.0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if (section == 1) {
        return 2;
    }
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if (section == 1) {
        return 2;
    }
    return 0;
}

#pragma mark - addUI
- (void)addConstrains{
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(64);
        make.width.equalTo(self.view);
        make.bottom.equalTo(self.mas_bottomLayoutGuide);
        make.centerX.equalTo(self.view);
    }];
    
    [self.changePierBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.naviHeader.leftLabel);
        make.left.equalTo(self.naviHeader).offset(15);
    }];
    
    [self.userStateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.naviHeader.leftLabel);
        make.right.equalTo(self.naviHeader).offset(-15);
    }];
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        [_collectionView registerClass:[SLHomeCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [_collectionView registerClass:[SLHomeHeadView class] forCellWithReuseIdentifier:@"cell1"];
        _collectionView.backgroundColor = [UIColor defeutBackgroudColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

- (SLInformationViewModel *)infoViewModel{
    if (!_infoViewModel) {
        _infoViewModel = [SLInformationViewModel new];
    }
    return _infoViewModel;
}
- (NSMutableArray *)photoList
{
    if (!_photoList) {
        _photoList = [@[@{@"text":@"附近找货",@"image":@"ic_index_fujinzhaohuo"},
                        @{@"text":@"航线找货",@"image":@"ic_index_hangxianzhaohuo"},
                        @{@"text":@"一键搜索",@"image":@"ic_index_yijiansousuo"},
                        @{@"text":@"关注货主",@"image":@"ic_index_guanzhuhuozhu"},
                        @{@"text":@"最新消息",@"image":@"ic_index_zuixinxiaoxi"},
                        @{@"text":@"特惠活动",@"image":@"ic_index_tehuihuodong"},
                        ] mutableCopy];
    }
    return _photoList;
}
- (NSMutableArray *)scrollListImg
{
    if (!_scrollListImg) {
        _scrollListImg = [NSMutableArray arrayWithCapacity:1];
    }
    return _scrollListImg;
}
- (UIButton *)changePierBtn{
    if (!_changePierBtn) {
    _changePierBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    _changePierBtn.titleLabel.font = bFont(20);
    [_changePierBtn setTitle:@"新码头" forState:UIControlStateNormal];
    [_changePierBtn setTitleColor:RGBCOLOR(253,207,0) forState:UIControlStateNormal];
    [_changePierBtn addTarget:self action:@selector(changeUserState) forControlEvents:UIControlEventTouchUpInside];
    }
    return _changePierBtn;
}

- (UIButton *)userStateBtn{
    if (!_userStateBtn) {
        _userStateBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
        _userStateBtn.titleLabel.font = bFont(20);
        [_userStateBtn setTitleColor:RGBCOLOR(253,207,0) forState:UIControlStateNormal];
    }
    return _userStateBtn;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
